package cn.edu.scau.sec.caixiaoqing.client;

import cn.edu.scau.sec.caixiaoqing.abstractFactory.AbstractFactory;
import cn.edu.scau.sec.caixiaoqing.simpleFactoryMethod.factory.ConsumerElectronicsFactory;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

/**
 * 这是我做的关于工厂模式的小demo，本想着按照老师的例子思路打一下发现代码还是有点耦合度（比如工厂模式工厂的抽象父类中的筛选方法，
 * 每次多了一个新产品就需要对其进行修改，就觉得这一块不太好），奔着写出来的代码就尽量不需要修改的思路，我就想着使用配置文件加反射
 * 进行较为简单的解耦，大体的代码与老师的例子一致
 */
public class FactoryClient {
    //引入配置文件
    private static Properties properties = new Properties();

    public static void main(String[] args) {

        ClassLoader loader = FactoryClient.class.getClassLoader();
        InputStream stream = loader.getResourceAsStream("brand&consumer.properties");
        try {
            properties.load(stream);
        } catch (IOException e) {
            e.printStackTrace();
        }
        //测试简单工厂模式
        testSimpleFactory();


        try {
            //测试工厂模式
            testFactoryPattern();

            //测试抽象工厂模式
            testAbstractFactory();
        } catch (Exception e) {
            e.printStackTrace();
        }



    }

    //简单工厂模式测试方法
    private static void testSimpleFactory(){
        ConsumerElectronicsFactory.getConsumerElectronics(ConsumerElectronicsFactory.id_Pad).recreation();
        ConsumerElectronicsFactory.getConsumerElectronics(ConsumerElectronicsFactory.id_Watch).recreation();
        ConsumerElectronicsFactory.getConsumerElectronics(ConsumerElectronicsFactory.id_Phone).recreation();
    }

    //工厂模式测试方法
    private static void testFactoryPattern() throws Exception {
        cn.edu.scau.sec.caixiaoqing.factoryPattern.factory.ConsumerElectronicsFactory.getConsumerElectronicsFactory(properties.getProperty("pad")).getConsumerElectronics().recreation();
        cn.edu.scau.sec.caixiaoqing.factoryPattern.factory.ConsumerElectronicsFactory.getConsumerElectronicsFactory(properties.getProperty("watch")).getConsumerElectronics().recreation();
        cn.edu.scau.sec.caixiaoqing.factoryPattern.factory.ConsumerElectronicsFactory.getConsumerElectronicsFactory(properties.getProperty("phone")).getConsumerElectronics().recreation();
    }


    //抽象工厂测试方法
    private static void testAbstractFactory() throws Exception {
        AbstractFactory.getAbstractFactory(properties.getProperty("apple")).getConsumerElectronics(properties.getProperty("PAD")).recreation();
        AbstractFactory.getAbstractFactory(properties.getProperty("apple")).getConsumerElectronics(properties.getProperty("WATCH")).recreation();
        AbstractFactory.getAbstractFactory(properties.getProperty("apple")).getConsumerElectronics(properties.getProperty("PHONE")).recreation();

        AbstractFactory.getAbstractFactory(properties.getProperty("huawei")).getConsumerElectronics(properties.getProperty("PAD")).recreation();
        AbstractFactory.getAbstractFactory(properties.getProperty("huawei")).getConsumerElectronics(properties.getProperty("WATCH")).recreation();
        AbstractFactory.getAbstractFactory(properties.getProperty("huawei")).getConsumerElectronics(properties.getProperty("PHONE")).recreation();

        AbstractFactory.getAbstractFactory(properties.getProperty("mi")).getConsumerElectronics(properties.getProperty("PAD")).recreation();
        AbstractFactory.getAbstractFactory(properties.getProperty("mi")).getConsumerElectronics(properties.getProperty("WATCH")).recreation();
        AbstractFactory.getAbstractFactory(properties.getProperty("mi")).getConsumerElectronics(properties.getProperty("PHONE")).recreation();

    }

}
